package com.denimgroup.threadfix.data.entities;

import java.util.Calendar;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;

import org.codehaus.jackson.annotate.JsonIgnore;


// TODO add mappings for commented sections
@Entity
@Table(name = "DeletedVulnerability")
public class DeletedVulnerability extends AuditableEntity {

	private static final long serialVersionUID = 7131204375011911346L;

	public DeletedVulnerability(Vulnerability vulnerability) {
		
		if (vulnerability != null) {
			setGenericVulnerability(vulnerability.getGenericVulnerability());
			setGenericSeverity(vulnerability.getGenericSeverity());
			setVariableHash(vulnerability.getVariableHash());
			setLocationVariableHash(vulnerability.getLocationVariableHash());
			setLocationHash(vulnerability.getLocationHash());
			setActive(vulnerability.isActive());
			setIsFalsePositive(vulnerability.getIsFalsePositive());
			setWafRuleGeneratedTime(vulnerability.getWafRuleGeneratedTime());
			setDefectSubmittedTime(vulnerability.getDefectSubmittedTime());
			setDefectClosedTime(vulnerability.getDefectClosedTime());
			setOpenTime(vulnerability.getOpenTime());
			setCloseTime(vulnerability.getCloseTime());
			setExpired(vulnerability.isExpired());
			setFoundByScanner(vulnerability.isFoundByScanner());
			setId(vulnerability.getId());

			if (vulnerability.getApplication() != null) {
				setApplicationId(vulnerability.getApplication().getId());
			}
			
			if (vulnerability.getDefect() != null) {
				setDefectId(vulnerability.getDefect().getId());
			}
			
			if (vulnerability.getSurfaceLocation() != null) {
				setSurfaceLocationId(vulnerability.getSurfaceLocation().getId());
			}
		}
	}
	
	private Integer applicationId;
	private GenericVulnerability genericVulnerability;
	private GenericSeverity genericSeverity;
	
	@Size(max = 128, message = "{errors.maxlength} 128.")
	private String variableHash;
	
	@Size(max = 128, message = "{errors.maxlength} 128.")
	private String locationVariableHash;
	
	@Size(max = 128, message = "{errors.maxlength} 128.")
	private String locationHash;
	
	private boolean isFalsePositive = false;
	private Calendar wafRuleGeneratedTime;
	private Calendar defectSubmittedTime;
	private Calendar defectClosedTime;
	private Calendar openTime;
	private Calendar closeTime;
	private boolean foundByScanner = true;
	private boolean expired = false;
	private int defectId;
	private int surfaceLocationId;

	@Column
	public Integer getApplicationId() {
		return applicationId;
	}

	public void setApplicationId(Integer applicationId) {
		this.applicationId = applicationId;
	}

	@ManyToOne
	@JoinColumn(name = "genericVulnerabilityId")
	public GenericVulnerability getGenericVulnerability() {
		return genericVulnerability;
	}

	public void setGenericVulnerability(GenericVulnerability genericVulnerability) {
		this.genericVulnerability = genericVulnerability;
	}

	@ManyToOne
	@JoinColumn(name = "genericSeverityId")
	public GenericSeverity getGenericSeverity() {
		return genericSeverity;
	}

	public void setGenericSeverity(GenericSeverity genericSeverity) {
		this.genericSeverity = genericSeverity;
	}

	@Column(length = 128, nullable = true)
	@JsonIgnore
	public String getVariableHash() {
		return variableHash;
	}

	public void setVariableHash(String variableHash) {
		this.variableHash = variableHash;
	}

	@Column(length = 128, nullable = true)
	@JsonIgnore
	public String getLocationVariableHash() {
		return locationVariableHash;
	}

	public void setLocationVariableHash(String locationVariableHash) {
		this.locationVariableHash = locationVariableHash;
	}

	@Column(length = 128, nullable = true)
	@JsonIgnore
	public String getLocationHash() {
		return locationHash;
	}

	public void setLocationHash(String locationHash) {
		this.locationHash = locationHash;
	}
	
	@Column(nullable = false)
	public boolean getIsFalsePositive() {
		return isFalsePositive;
	}

	public void setIsFalsePositive(boolean isFalsePositive) {
		this.isFalsePositive = isFalsePositive;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@JsonIgnore
	public Calendar getWafRuleGeneratedTime() {
		return wafRuleGeneratedTime;
	}

	public void setWafRuleGeneratedTime(Calendar wafRuleGeneratedTime) {
		this.wafRuleGeneratedTime = wafRuleGeneratedTime;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@JsonIgnore
	public Calendar getDefectSubmittedTime() {
		return defectSubmittedTime;
	}

	public void setDefectSubmittedTime(Calendar defectSubmittedTime) {
		this.defectSubmittedTime = defectSubmittedTime;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@JsonIgnore
	public Calendar getDefectClosedTime() {
		return defectClosedTime;
	}

	public void setDefectClosedTime(Calendar defectClosedTime) {
		this.defectClosedTime = defectClosedTime;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	public Calendar getOpenTime() {
		return openTime;
	}

	public void setOpenTime(Calendar openTime) {
		this.openTime = openTime;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Calendar getCloseTime() {
		return closeTime;
	}

	public void setCloseTime(Calendar closeTime) {
		this.closeTime = closeTime;
	}

	@Column(nullable = false)
	@JsonIgnore
	public boolean isExpired() {
		return expired;
	}

	public void setExpired(boolean isExpired) {
		this.expired = isExpired;
	}
	
	/**
	 * This indicates whether the user has closed the vulnerability.
	 * @return
	 */
	@Column(nullable = false)
	@JsonIgnore
	public boolean isFoundByScanner() {
		return foundByScanner;
	}
	
	public void setFoundByScanner(boolean isFoundByScanner) {
		this.foundByScanner = isFoundByScanner;
	}

	public int getDefectId() {
		return defectId;
	}

	public void setDefectId(int defectId) {
		this.defectId = defectId;
	}

	public int getSurfaceLocationId() {
		return surfaceLocationId;
	}

	public void setSurfaceLocationId(int surfaceLocationId) {
		this.surfaceLocationId = surfaceLocationId;
	}	
}
